import 'package:city_pickers/city_pickers.dart';
import 'package:city_pickers/modal/result.dart';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:fluro/fluro.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_switch/flutter_switch.dart';
import 'package:shenfuhui/config/config.dart';

import '../routers/routers.dart';
import '../tabview/order_list.dart';
import '../views/dialog_manager.dart';

///
/// 商品专区
///
class ProductSpecialAreaPage extends StatefulWidget {
  String pageTitle = "";

  ProductSpecialAreaPage({Key? key, this.pageTitle = ""}) : super(key: key);

  @override
  State<ProductSpecialAreaPage> createState() => _ProductSpecialAreaPageState();
}

class _ProductSpecialAreaPageState extends State<ProductSpecialAreaPage> {
  var tabNames = [
    '国外精品',
    '母婴用品',
    '电子科技',
    '箱包皮具',
    '服饰鞋帽',
  ];

  int _currentIndex = 0;

  //返回菜单item
  getMenuItem(int position) {
    return InkWell(
      onTap: () {
        setState(() {
          _currentIndex = position;
        });
      },
      child: Container(
        width: 150.w,
        decoration: BoxDecoration(color: Colors.transparent),
        padding: EdgeInsets.fromLTRB(0.w, 0, 0.w, 0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            _currentIndex == position
                ? Text(
                    tabNames[position],
                    style:
                        TextStyle(color: (Config.colorMain), fontSize: 25.sp),
                  )
                : Text(
                    tabNames[position],
                    style:
                        TextStyle(color: (Config.colorText1), fontSize: 25.sp),
                  ),
            Opacity(
              opacity: _currentIndex == position ? 1 : 0,
              child: Container(
                width: 50.w,
                margin: EdgeInsets.only(top: 6.w),
                child: Divider(
                  color: Config.colorMain,
                  height: 3.w,
                  thickness: 3.w,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  //返回商品列表item
  getListItem(int index) {
    return InkWell(
      onTap: () {
        Routes.navigateTo(context, Routes.productDetail,
            params: {'productId': '001'});
      },
      child: Container(
        decoration: BoxDecoration(
          color: Config.colorWhite,
          borderRadius: BorderRadius.circular(10.w),
        ),
        child: Column(
          children: [
            ClipRRect(
              borderRadius: BorderRadius.circular(10.w),
              child: Image.network(
                'https://bkimg.cdn.bcebos.com/pic/dc54564e9258d109b3de9cb75d14dbbf6c81800ae728',
                fit: BoxFit.fill,
                height: 320.w,
              ),
            ),
            Padding(padding: EdgeInsets.all(5.w)),
            Container(
              padding: EdgeInsets.symmetric(horizontal: 15.w),
              width: double.infinity,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(padding: EdgeInsets.only(top: 2.w)),
                  Text(
                    '商品标题',
                    style: TextStyle(color: Config.colorText1, fontSize: 28.sp),
                  ),
                  Padding(padding: EdgeInsets.only(top: 10.w)),
                  Text(
                    '商品一句话简介',
                    style: TextStyle(color: Config.colorText3, fontSize: 24.sp),
                    overflow: TextOverflow.ellipsis,
                    maxLines: 1,
                  ),
                  Padding(padding: EdgeInsets.only(top: 10.w)),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text(
                        '￥0.00',
                        style:
                            TextStyle(color: Colors.redAccent, fontSize: 24.sp),
                      ),
                      Text(
                        '库存1',
                        style: TextStyle(
                            color: Config.colorText3, fontSize: 24.sp),
                      ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Config.colorBg2,
      appBar: AppBar(
        title: Text(widget.pageTitle),
        centerTitle: true,
      ),
      body: Container(
        width: double.infinity,
        height: double.infinity,
        child: Stack(
          children: [
            Container(
              decoration: BoxDecoration(color: Config.colorWhite),
              width: double.infinity,
              height: 65.w,
              child: ListView.builder(
                itemCount: tabNames.length,
                padding: const EdgeInsets.symmetric(vertical: 0, horizontal: 0),
                shrinkWrap: true,
                scrollDirection: Axis.horizontal,
                physics: const BouncingScrollPhysics(),
                itemBuilder: (BuildContext context, int index) {
                  return getMenuItem(index);
                },
              ),
            ),
            Container(
              height: double.infinity,
              width: double.infinity,
              margin: EdgeInsets.only(top: 65.w),
              child: EasyRefresh(
                header: const MaterialHeader(),
                footer: const MaterialFooter(),
                onRefresh: () async {
                  await Future.delayed(const Duration(seconds: 1), () {
                    print('refresh');
                  });
                },
                onLoad: () async {
                  await Future.delayed(const Duration(seconds: 1), () {
                    print('load');
                  });
                },
                child: GridView.builder(
                  padding:
                      EdgeInsets.symmetric(horizontal: 25.w, vertical: 20.w),
                  itemCount: 10,
                  // shrinkWrap: true,
                  // scrollDirection: Axis.vertical,
                  // physics: const BouncingScrollPhysics(),
                  itemBuilder: (BuildContext context, int index) {
                    return getListItem(index);
                  },
                  gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                      crossAxisCount: 2,
                      mainAxisSpacing: 19.w,
                      crossAxisSpacing: 19.w,
                      childAspectRatio: 0.72),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
